/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork.Objects.Items;

import FrameWork.Audio.RSAudio;
import FrameWork.GameScreen;
import FrameWork.Objects.Character.Player;
import FrameWork.Objects.MyObject;
import FrameWork.PSboombubble;
import FrameWork.Point;
import FrameWork.Rectangle;
import GamePlay.Global;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author ThanhTri
 */
public class boom extends Items {
    private int     m_Lenght;
    private Vector  m_vBoom;
    private Vector  m_ListItem;
    private int     m_StateExplosion;
    private float   m_TimeExplosion;
    private boolean m_iMove;
    private int     m_Direction;
    private float   m_CurX;
    private float   m_CurY;
    private PSboombubble m_pBubble;
    public boom(Vector _ListItem,int _Lenght,int _X,int _Y, int _SizeX, int _SizeY,
            float _TimeAni, 
            int _Map[][],int _Width, int _Height,
            int _ID){     
        super(_X, _Y, _SizeX, _SizeY, _TimeAni, _Map, _Width, _Height, _ID);
        m_Lenght            = _Lenght;
        m_StateExplosion    = 0;
        m_TimeExplosion     = 0;
        m_vBoom             = new Vector();
        m_iMove             = false;
        m_Direction         = Global.DIR_DOWN;
        m_CurX  =   0;
        m_CurY  =   0;
        m_ListItem =  _ListItem;
        m_CurFrame = 12;
    }
    public int getExplosion (){
        return m_StateExplosion;
    }
     public int getLenght (){
        return m_Lenght;
    }
     public int getCurFrame (){
         return m_CurFrame;
     }
    public void ActionCollision (MyObject _Object){
    }
    public void Update(float _Time,int _Terrain[][]) {
        super.Update(_Time,_Terrain);
        switch(m_STATUS){
            case Global.STT_ACTIVE:
                    m_TimeUpdate+=_Time;
                    if(m_iMove){
                        Move(_Time,_Terrain);
                    }
                    if(m_TimeUpdate>=2.5f){
                        RSAudio.getRS().PlayExplosion();
                        m_TimeUpdate = 0;
                        m_STATUS= Global.STT_DEATH;  
                        
                        //<editor-fold defaultstate="collapsed" desc="right">
                        for (int i = 1; i <= m_Lenght; i++) {
                            int dx = m_X+i;
                            int dy  = m_Y;
                            if (dx<0 || dx >= m_WidthMap 
                                    || dy<0 || dy >=m_HeightMap){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_GRASS){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_DOOR){
                                break;
                            }
                            m_vBoom.addElement(new Point(dx, dy));
                        }
                        //</editor-fold>
                        
                        //<editor-fold defaultstate="collapsed" desc="Left">
                        for (int i = 1; i <= m_Lenght; i++) {
                             int dx = m_X-i;
                            int dy  = m_Y;
                            if (dx<0 || dx >= m_WidthMap 
                                    || dy<0 || dy >=m_HeightMap){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_GRASS){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_DOOR){
                                break;
                            }
                            m_vBoom.addElement(new Point(dx,dy));
                        }
                        //</editor-fold>
                        
                        //<editor-fold defaultstate="collapsed" desc="Up">
                        for (int i = 1; i <= m_Lenght; i++) {
                            int dx = m_X;
                            int dy  = m_Y-i;
                            if (dx<0 || dx >= m_WidthMap 
                                    || dy<0 || dy >=m_HeightMap){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_GRASS){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_DOOR){
                                break;
                            }
                            m_vBoom.addElement(new Point(dx, dy));
                        }
                        //</editor-fold>
                        
                        //<editor-fold defaultstate="collapsed" desc="Down">
                        for (int i = 1; i <= m_Lenght; i++) {
                            int dx = m_X;
                            int dy  = m_Y+i;
                            if (dx<0 || dx >= m_WidthMap 
                                    || dy<0 || dy >=m_HeightMap){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_GRASS){
                                break;
                            }
                            if (_Terrain[dx][dy]== Global.ID_DOOR){
                                break;
                            }
                            m_vBoom.addElement(new Point(dx, dy));
                        }
                        //</editor-fold>
                        
                        m_ListItem.addElement(new BoomExplosion(this, m_X, m_Y, 
                                    30, 30, m_TimeAni, 
                                    m_Map, m_WidthMap, m_Lenght,
                                    Global.ID_BOOM_EXPLOSION));
                        for (int i = 0; i < m_vBoom.size(); i++) {
                            Point p = (Point)m_vBoom.elementAt(i);
                            m_ListItem.addElement(new BoomExplosion(this, p.X, p.Y, 
                                    30, 30, m_TimeAni, 
                                    m_Map, m_WidthMap, m_Lenght,
                                    Global.ID_BOOM_EXPLOSION));
                        }
            try {
                m_pBubble = new PSboombubble(m_X, m_Y, m_vBoom, m_Lenght);
                m_pBubble.Init();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
                    }break;
            case Global.STT_DEATH:
                m_TimeUpdate+=_Time;
                m_pBubble.Update(_Time);
                if(m_TimeUpdate>=0.3f){
                    m_TimeUpdate =0;
                     m_OldStatus = Global.STT_DEATH; 
                }
        }
    }
    public void UpdateAnimation (float _Time){
        m_SaveTimeAni += _Time;
        
        switch(m_STATUS){
            case Global.STT_ACTIVE:
                if (m_SaveTimeAni>= 0.1f){
                    m_SaveTimeAni = 0;
                    m_CurFrame =(m_CurFrame+1)%6 + 12;
                }
                break;
        }
    }
    public void DrawScreen (Graphics _Graphics,GameScreen _Screen){
        switch(m_STATUS){
            case Global.STT_ACTIVE:
                if(m_iMove){
                    m_Sprite.setFrame(m_CurFrame);

                    m_Sprite.setPosition(m_X * 30+(int) m_CurX - (_Screen.getXView() - _Screen.getWidth()/2), 
                    m_Y * 30+(int) m_CurY - (_Screen.getYView() - _Screen.getHeight()/2));
        
                    m_Sprite.paint(_Graphics);
                    }else{
                    super.DrawScreen(_Graphics, _Screen);
                }
                break;
            case Global.STT_DEATH:
                m_pBubble.DrawScreen(_Graphics, _Screen);
                break;
                        
        }
    }
    public void Move (float _Time,int _Terrain[][]){
        int dX=0;
        int dY=0;
        switch(m_Direction){
            case Global.DIR_LEFT:
                if(m_X >= 1){
                    dX= -1;
                }
                break;
            case Global.DIR_RIGHT:
                if(m_X <m_WidthMap-1){
                    dX = 1;
                }
                break;
            case Global.DIR_UP:
                if(m_Y>=1) {
                    dY = -1;
                }
                
                break;
            case Global.DIR_DOWN:
                if(m_Y < m_HeightMap-1){
                    dY = 1;
                }
                break;
        }
        if (_Terrain[m_X+dX][m_Y+dY]==Global.ID_GRASS    ||
            _Terrain[m_X+dX][m_Y+dY]==Global.ID_BOOM     ||
            _Terrain[m_X+dX][m_Y+dY]==Global.ID_HOUSE     ||
            _Terrain[m_X+dX][m_Y+dY]==Global.ID_DOOR  ){
            dX=0;
            dY=0;
        }
        if (dX==0 && dY==0){
            m_iMove = false;
        }
        
        m_CurX+= dX*200*_Time;
        m_CurY+= dY*200*_Time;
       
       if (dX<0){
           if((m_CurX)<=-30){
               m_X --;
               m_CurX +=30;
           }
       }else{
           if (dX>0){
               if((m_CurX )>=30){
                   m_X ++;
                   m_CurX -=30;
               }
           }
       }
       
       
       if (dY<0){
           if((m_CurY )<=-30){
               m_Y --;
               m_CurY +=30;
           }
       }else{
           if (dY>0){
               if((m_CurY )>=30){
                   m_Y ++;
                   m_CurY -=30;
               }
           }
       }
    }
    public void ActiveMove (int _Direct){
        if (!m_iMove){
        m_iMove = true;
        m_Direction = _Direct;
        }
    }
    
    public void Active (Player _Player){
        if(_Player.getKick()){
            ActiveMove(_Player.getDirection());
        }
    }
    
    public boolean Intersect(Rectangle _rect){
        if (getRect().Intersects(_rect)){
            return true;
        }
        for (int i = 0; i < m_vBoom.size(); i++) {
            Point p = (Point)m_vBoom.elementAt(i);
            
            Rectangle rps = new Rectangle ((int)p.X*30,(int)p.Y*30,
                30,30);
            if (rps.Intersects(_rect)){
                return true;
            }
        }
        return false;
    }
    
}
